package ru.spbu.math.android.cbrf.view;

import java.util.List;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.dataprovider.ProviderManager;
import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.RateType;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * This class is OnItemClickListener for list of categories in ItemConfigurationActivity.
 * It watches over selecting Categories and open RateType list dialog
 * 
 * @author Olga Nikonova
 */
public class OnCategoryClickListener implements OnItemClickListener {

	private static final String TAG = "CategoryList";
	private CategoryListAdapter categoryListAdapter;
	private Context context;

	public OnCategoryClickListener(Context context,
			CategoryListAdapter listAdapter) {
		this.categoryListAdapter = listAdapter;
		this.context = context;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

//	TODO	ProgressDialog loadingDialog = ProgressDialog.show(context, "", 
//		        context.getString(R.string.Loading));
		
		Category category = (Category) categoryListAdapter.getItem(arg2);
		Log.d(TAG, "select category: " + category.getName());
		List<RateType> rateTypes = ProviderManager.getInstance(context)
				.getRateTypeByCategory(category);

		// set selected category in category list adapter
		categoryListAdapter.setSelectedCategory(category);

		// open RateTypes list dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(context.getResources().getString(R.string.Select));
		RateTypeListAdapter rateTypeAdapter = new RateTypeListAdapter(context,
				rateTypes, categoryListAdapter);
		builder.setAdapter(rateTypeAdapter, new OnRateTypeClickListener(
				rateTypeAdapter));
		
//		loadingDialog.cancel();
		builder.show();
	}

}
